package com.marker.mo.amapsmoothmarker;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Pair;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.AlphaAnimation;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.RotateAnimation;
import com.amap.api.maps.utils.SpatialRelationUtil;
import com.amap.api.maps.utils.overlay.SmoothMoveMarker;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * author: Ivan Zhang
 * 高德地图，图标平滑移动功能Demo
 */
public class MainActivity1 extends AppCompatActivity {

    private MapView mMapView = null;
    private AMap aMap;
    private List<MapBean> carsLatLng;//静态车辆的数据
    private List<MapBean> goLatLng;
    private List<Marker> showMarks;//静态车辆图标
    private List<SmoothMoveMarker> smoothMarkers;//平滑移动图标集合
    //经度
    private double lng = 0.0;
    //纬度
    private double lat = 0.0;
    private static final int TIMER = 999;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        initView();
        initData();
    }

    private void initView() {
        Button put = (Button) findViewById(R.id.put);
        Button run = (Button) findViewById(R.id.run);

        //放入静态车辆
        put.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearData();
                //依次放入静态图标
//                for (int i = 0; i < carsLatLng.size(); i++) {
                BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.car_up_white);
                lng = carsLatLng.get(0).latLng.longitude;
                lat = carsLatLng.get(0).latLng.latitude;
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(new LatLng(lat, lng))
                        .icon(icon);
                showMarks.add(aMap.addMarker(markerOptions));
                Animation startAnimation = new AlphaAnimation(0, 1);
                startAnimation.setDuration(600);
                //设置所有静止车的角度
//                            showMarks.get(i).setRotateAngle(Float.valueOf(listBaseBean.datas.get(i).angle));
                showMarks.get(0).setAnimation(startAnimation);
//                    showMarks.get(i).setRotateAngle(carsLatLng.get(i).getAngle());
                showMarks.get(0).startAnimation();
            }
//            }
        });

        /**
         * 展示平滑移动车辆
         */
        run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mHandler.postDelayed(runnable, 100);

            }
        });
    }

    private void clearData() {
        //清空地图覆盖物
        if (smoothMarkers == null) {
            smoothMarkers = new ArrayList<>();
        } else {
            for (int i = 0; i < smoothMarkers.size(); i++) {
                smoothMarkers.get(i).destroy();
            }
            smoothMarkers.clear();
        }
        //清除旧集合
        if (showMarks == null) {
            showMarks = new ArrayList<Marker>();
        } else {
            for (int i = 0; i < showMarks.size(); i++) {
                showMarks.get(i).destroy();
            }
            showMarks.clear();
        }
    }

    private Handler mHandler = new Handler();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            carsLatLng = goLatLng;
            goLatLng = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                Random ran = new Random(System.currentTimeMillis());
                double lat = 39.908172 + (ran.nextInt(10) * 0.05);
                double lng = 116.3500 + (ran.nextInt(10) * 0.01);
                float angle = new Random().nextInt(359);
                LatLng go = new LatLng(lat, lng);
                MapBean bean = new MapBean(angle, go);
                goLatLng.add(bean);
            }
            clearData();
            //循环
            for (int i = 0; i < 5; i++) {
                //移动车辆
                movePoint(i);
            }
            //在这里执行定时需要的操作
            mHandler.postDelayed(this, 5000);
        }
    };

    private void initData() {
//        //出发地
//        LatLng car1 = new LatLng(39.902138, 116.391415);
//        LatLng car2 = new LatLng(39.935184, 116.328587);
//        LatLng car3 = new LatLng(39.987814, 116.488232);
//        //出发地坐标集合
//        carsLatLng = new ArrayList<>();
//        carsLatLng.add(car1);
//        carsLatLng.add(car2);
//        carsLatLng.add(car3);

//        //目的地
//        LatLng go1 = new LatLng(39.96782, 116.403775);
//        LatLng go2 = new LatLng(39.891225, 116.322235);
//        LatLng go3 = new LatLng(39.883322, 116.415619);
//        //目的地坐标集合
//        goLatLng = new ArrayList<>();
//        goLatLng.add(go1);
//        goLatLng.add(go2);
//        goLatLng.add(go3);


        carsLatLng = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Random ran = new Random(System.currentTimeMillis());
            double lat = 39.908172 + (ran.nextInt(10) * 0.05);
            double lng = 116.3500 + (ran.nextInt(10) * 0.01);
            float angle = new Random().nextInt(359);
            LatLng car = new LatLng(lat, lng);
            MapBean bean = new MapBean(angle, car);
            carsLatLng.add(bean);
        }
        LogUtil.e("起点" + carsLatLng.get(0).latLng.latitude + " " + carsLatLng.get(0).latLng.longitude + " 角度" + carsLatLng.get(0).getAngle());
        goLatLng = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Random ran = new Random(System.currentTimeMillis());
            double lat = 39.908172 + (ran.nextInt(10) * 0.05);
            double lng = 116.3500 + (ran.nextInt(10) * 0.01);
            float angle = new Random().nextInt(359);
            LatLng go = new LatLng(lat, lng);
            MapBean bean = new MapBean(angle, go);
            goLatLng.add(bean);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    //平滑移动
    public void movePoint(int i) {
        BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.car_up_white);
        // 获取轨迹坐标点
        List<LatLng> points = new ArrayList<LatLng>();
        points.add(carsLatLng.get(i).latLng);
        points.add(goLatLng.get(i).latLng);
        RotateAnimation rotateAnimation = new RotateAnimation(360.0F - carsLatLng.get(i).getAngle() + aMap.getCameraPosition().bearing, 360.0F - goLatLng.get(i).getAngle() + aMap.getCameraPosition().bearing);
        LinearInterpolator lin = new LinearInterpolator();
        rotateAnimation.setInterpolator(lin); //设置插值器
        rotateAnimation.setDuration(1000);
        SmoothMoveMarker smoothMarker = new SmoothMoveMarker(aMap);
        smoothMarkers.add(smoothMarker);
        // 设置滑动的图标
        smoothMarkers.get(i).setDescriptor(icon);
        // 取轨迹点的第一个点 作为 平滑移动的启动
        LatLng drivePoint = carsLatLng.get(i).latLng;
//        LogUtil.e("第一个点" + points.get(i).latitude + " " + points.get(i).longitude + " 角度" + carsLatLng.get(i).getAngle());
        Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(points, drivePoint);
        points.set(pair.first, drivePoint);
        List<LatLng> subList = points.subList(pair.first, points.size());

        // 设置滑动的轨迹左边点
        smoothMarkers.get(i).setPoints(subList);
        // 设置滑动的总时间
        smoothMarkers.get(i).setTotalDuration(5);
        // 开始滑动
        smoothMarkers.get(i).startSmoothMove();

//        smoothMarkers.get(i).getMarker().setRotateAngle(carsLatLng.get(i).getAngle());
//        smoothMarkers.get(i).getMarker().setAnimation(rotateAnimation);
//        smoothMarkers.get(i).getMarker().startAnimation();
    }

}